<?php

namespace Admin\Controller;
use Think\Controller;

class PlatformController extends Controller{
	

	/*添加分类*/

	public function insert_type(){

		$name = I('post.name');

		$type = M('game_type');

		$arr = $type->select();

		if($arr==null){
			$data['rank'] = 1;
		}else{
			foreach($arr as $v){

			$data['rank'] = $v['id']+1;
		}
		}

		if($arr==null){
			$data['rank'] =1;
		}

		$data['name'] = $name;

		$result = $type->add($data);

		if($result!=false){

			$ajaxReturn['code'] = 1;
			$ajaxReturn['msg'] = '添加成功！';

		}else{

			$ajaxReturn['code'] = 2;
			$ajaxReturn['msg'] = '添加失败！';

		}

		$this->ajaxReturn($ajaxReturn);

	}

	/*删除分类*/

	public function delete_type(){

		$id = I('post.id');

		$type = M('game_type');

		$result = $type->where('id='.$id)->delete();

		if($result!=false){

			$ajaxReturn['code'] = 1;
			$ajaxReturn['msg'] = '删除成功！';

		}else{

			$ajaxReturn['code'] = 2;
			$ajaxReturn['msg'] = '删除失败！';

		}

		$this->ajaxReturn($ajaxReturn);

	}

	/*展示分类*/

	public function list_type(){

		$page = I('get.page/d');//当前页数
        $page_size = I('get.page_size/d');


		$type = M('game_type');

		$count = $type->count();
        $total =  ceil($count / $page_size);
        $limitStart = ($page-1) * $page_size;
        
        $result=$type->order("id desc")->limit($limitStart,$page_size)->select();

		$result = $type->order('rank desc')->select();

		if($result !== false){

            $data['page']['total_page'] = $total;
            $data['list'] = $result;
            

            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data'] = $data;
        }else{

            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "数据查询失败！";
        }

        $this->ajaxReturn($ajaxReturn);

	}


	/*分类排序*/

	public function sort_type(){

		$thisid = I('post.id');
        $type   = I('post.type');
        $and    = $type == 1 ? '>' : '<';
        $order  = $type == 1 ? 'asc' : 'desc';

        $searchModel = M('game_type');

        $thisinfo   = $searchModel->where('id='.$thisid)->getField('rank,id');
        $thisrank   = array_keys($thisinfo)[0];
        $thiscid    = array_values($thisinfo)[0];

        $nextinfo   = $searchModel->where('rank'.$and.$thisrank)->order('rank '.$order)->getField('id,rank');

        $nextid     = array_keys($nextinfo)[0];
        $nextrank   = array_values($nextinfo)[0];

        if($thisrank && $nextid && $nextrank){

            $searchModel-> where('id='.$thisid)->setField('rank',$nextrank);
            $searchModel-> where('id='.$nextid)->setField('rank',$thisrank);

            $data['thisid'] = $thisid;
            $data['thisrank'] = $thisrank;
            $data['nextid'] = $nextid;
            $data['nextrank'] = $nextrank;

            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data'] = $data;
        }else{

            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "数据更新失败！";
        }

        $this->ajaxReturn($ajaxReturn);

	}
 
}